<template>
	<view class="content">
		<view class="info_div">

			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>
					<view style="visibility: hidden;">空</view>联系人 |
				</view>
				<input class="et" placeholder="请输入姓名" @input="onInputName" :value="info.name" />
			</view>

			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>联系电话 |
				</view>
				<input class="et" placeholder="请输入电话" @input="onInputPhone" :value="info.mobile" />
			</view>

			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>选择地区 |
				</view>
				<!-- #ifdef MP -->
				<picker mode="region" class="picker_region" :value="area" @change="onSelArea">
					<view>{{info.province}}-{{info.city}}-{{info.county}}</view>
				</picker>
				<!-- #endif -->
				<!-- #ifndef MP -->
				<view>仅小程序支持选择地区</view>
				<!-- #endif -->

				<!-- <uni-data-picker placeholder="请选择地址" popup-title="请选择城市" collection="opendb-city-china" field="code as value, name as text" orderby="value asc" :step-searh="true"
					self-field="code" parent-field="parent_code" @change="onchange" @nodeclick="onnodeclick">
				</uni-data-picker> -->

			</view>

			<view class="item_div_2">
				<view style="display: flex;">
					<view style="color: red;">*</view>详细地址 |
				</view>
				<textarea class="et_2" placeholder="请输入详细地址" @input="onInputAddr" :value="info.address" />
			</view>

			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>地理位置 |
				</view>
				<view class="et" @click.stop="onChooseLocation">{{map_position}} ></view>
			</view>

		</view>

		<!-- #ifdef MP -->
		<view class="btn_ok" @click.stop="onOk">确定</view>
		<!-- #endif -->
		<!-- #ifndef MP -->
		<view class="btn_ok" @click.stop="onOk(-1)">请使用小程序添加地址</view>
		<!-- #endif -->

	</view>
</template>

<script>
	import { getUInfo } from '@/util/util.js'
	export default {
		data() {
			return {
				info: {
					name: '',
					mobile: '',
					province: '山东省',
					city: '临沂市',
					county: '兰山区',
					address: '',
					latitude: 35.103628,
					longitude: 118.356410,
				},
				area: ['山东省', '临沂市', '兰山区'],

				map_position: '临沂市', // 地图选点位置
				location: {}, // 地理位置对象

			}
		},

		onLoad(opts) {
			if (opts.address) {
				let address = JSON.parse(opts.address)
				this.info = {
					name: address.name,
					mobile: address.mobile,
					province: address.province,
					city: address.city,
					county: address.county,
					address: address.address,
					latitude: address.latitude,
					longitude: address.longitude,
					id: address.id
				}
			}
		},

		methods: {

			// 打开地图选点
			onChooseLocation() {
				uni.chooseLocation({
					success: (res) => {
						console.log('地图选择位置的结果 = ', JSON.stringify(res));
						// console.log('位置名称：' + res.name);
						// console.log('详细地址：' + res.address);
						// console.log('纬度：' + res.latitude);
						// console.log('经度：' + res.longitude);
						this.map_position = res.name
						this.location = res
						this.info.longitude = res.longitude
						this.info.latitude = res.latitude

						// let analysisArea = getCityAndProvince(res.address)
						// if (analysisArea) {
						// 	if (analysisArea.length >= 2) {
						// 		this.province_str = analysisArea[0]
						// 		this.city_str = analysisArea[1]
						// 	} else if (analysisArea.length >= 1) {
						// 		this.province_str = analysisArea[0]
						// 		this.city_str = ''
						// 	} else if (analysisArea.length == 0) {
						// 		this.province_str = '山东省'
						// 		this.city_str = '临沂市'
						// 	}
						// } else {
						// 	this.province_str = '山东省'
						// 	this.city_str = '临沂市'
						// }

					},
					fail: (res) => {
						console.log('onChooseLocation.fail = ', JSON.stringify(res));
						uni.showToast({ title: '未获取到位置信息', icon: 'none' })
					}
				})

			},

			// 选择省市区
			onSelArea(e) {
				console.log('onSelArea = ', e.detail.value);
				this.area = e.detail.value
				this.info.province = this.area[0]
				this.info.city = this.area[1]
				this.info.county = this.area[2]
			},

			onInputPhone(e) {
				this.info.mobile = e.detail.value
			},

			// 输入姓名
			onInputName(e) {
				this.info.name = e.detail.value
			},

			// 输入详细地址
			onInputAddr(e) {
				this.info.address = e.detail.value
			},

			onchange(e) {
				console.log('e.detail.value = ', e.detail.value);
			},

			onnodeclick(node) {
				console.log('onnodeclick = ', node);
			},

			// 确认添加
			onOk(flag) {
				let that = this
				if (flag == -1) {
					uni.showToast({ icon: 'none', title: '请使用小程序添加地址' })
					return
				}
				if (!this.info.name) {
					uni.showToast({ icon: 'none', title: '请输入姓名' })
					return
				}
				if (!this.info.mobile) {
					uni.showToast({ icon: 'none', title: '请输入电话' })
					return
				}
				if (!this.info.address) {
					uni.showToast({ icon: 'none', title: '请输入详细地址' })
					return
				}

				(!this.info.id) && (delete this.info.id)

				uni.showModal({
					title: '提示',
					content: '确认提交地址信息？',
					success: (res) => {
						res.confirm && that.postAddress()
					}
				})
			},

			// 接口:添加/编辑地址 
			async postAddress() {
				let that = this
				let token = getUInfo().token
				uni.showLoading({ title: '加载中', mask: true, })
				let res = await that.$addAddress({ token, ...this.info })
				if (res.code == 1) {
					uni.$emit('onAddrAddEdit', { msg: 'onAddrAddEdit' })
					uni.showModal({ title: '提示', content: `${res.msg}`, showCancel: false, success: (res) => { res.confirm && uni.navigateBack() } })
				} else {
					uni.showToast({ title: res.msg, icon: 'none' })
				}
			},

		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	/* 分割线 */
	.divider {
		width: 100%;
		height: 1rpx;
		background-color: rgba(0, 0, 0, 0.2);
	}

	.info_div {
		display: flex;
		flex-direction: column;
		width: calc(100% - 40rpx);
		margin: 20rpx 20rpx 0 20rpx;
	}

	/* 每个填写项目的容器 */
	.item_div {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 60rpx;
		line-height: 60rpx;
		margin: 22rpx 0 0 0;
		font-size: 28rpx;
		/* background-color: red; */
		overflow: hidden;
	}

	/* 大一点的项目容器 */
	.item_div_2 {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 100rpx;
		/* line-height: 100rpx; */
		margin: 30rpx 0 0 0;
		font-size: 28rpx;
	}

	/* 预约时间项目的容器 */
	.item_div_3 {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 86rpx;
		line-height: 60rpx;
		margin: 22rpx 0 0 0;
		font-size: 28rpx;
		/* background-color: red; */
		overflow: hidden;
	}

	.et {
		/* background-color: red; */
		flex-grow: 1;
		margin: 0 0 0 0rpx;
		max-width: 70%;
		height: 60rpx;
		text-align: right;
		font-size: 28rpx;
	}

	.et_2 {
		/* background-color: red; */
		flex-grow: 1;
		margin: 0 0 0 0rpx;
		max-width: 70%;
		height: 100rpx;
		/* line-height: 100rpx; */
		text-align: right;
		font-size: 28rpx;
		/* border: solid 1rpx gray; */
		/* vertical-align: middle; */
	}

	.et_3 {
		display: flex;
		/* background-color: red; */
		flex-grow: 1;
		margin: 0 0 0 0rpx;
		max-width: 70%;
		height: 60rpx;
		font-size: 28rpx;
		/* background-color: red; */
	}

	.picker_region {
		width: 70%;
		height: 100%;
		text-align: right;
		/* background: red; */
	}

	.btn_ok {
		align-self: center;
		background: #F7D34B;
		width: 90%;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
		margin: 88rpx 0 0 0;
		border-radius: 12rpx;
	}
</style>